package com.lianbang.pharmacy.module.medicine.list

import com.lianbang.pharmacy.R
import com.lianbang.pharmacy.databinding.FragmentMedicineListBinding
import com.lianbang.pharmacy.vm.MedicineVM
import android.os.Bundle
import android.view.View
import cn.ubest.mvvmcore.base.BaseListFragment
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lianbang.pharmacy.adapter.MedicineListAdapter
import com.lianbang.pharmacy.bean.MedicineListBean
import com.lianbang.pharmacy.module.main.MedicineSupplierActivity

class MedicineListFragment : BaseListFragment<MedicineVM,
        FragmentMedicineListBinding, MedicineListBean>() {
    private var mWord = ""

    companion object {
        fun newInstance(word: String): MedicineListFragment {
            val fragment = MedicineListFragment()
            fragment.arguments = Bundle().run {
                putString("word", word)
                this
            }
            return fragment
        }
    }

    override fun initLayout(): Int {
        return R.layout.fragment_medicine_list;
    }

    override fun initData(arguments: Bundle?) {
        super.initData(arguments)
        mWord = arguments?.getString("word", "") ?: ""
    }

    override fun initView(view: View) {
        super.initView(view)
    }

    override fun loadData() {
        mViewModel.refreshData(mWord)
    }

    override fun initAdapter(): BaseQuickAdapter<MedicineListBean, *> {
        return MedicineListAdapter().run {
            setOnItemClickListener { adapter, view, position ->
                val any = adapter.data[position] as MedicineListBean
                MedicineSupplierActivity.start(activity, any.id)
            }
            this
        }
    }

    fun refreshList(word: String) {
        mViewModel.refreshData(word)
    }


}